package com.dubbo.lian.common.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;

import java.util.Map;

/**
 * Created by Administrator on 2016/12/16 0016.
 */
public class ReturnTemplateUtil {
    static String version = "2.0";

    public static BaseR returnOfObject(Object obj){
        BaseR b = baseModeBuild();
        b.setContent(obj);
        return b;
    }

    public static BaseR returnOfObjectTimeList(Object obj){
        BaseR b = baseModeBuild();
        JSONArray json = null;
        try {
            String Jsonstr = JSONArray.toJSONStringWithDateFormat(obj,"yyyy-MM-dd");
            json = JSONObject.parseArray(Jsonstr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        b.setContent(json);
        return b;
    }

    public static BaseR returnOfObjectTime(Object obj){
        BaseR b = baseModeBuild();
        String Jsonstr = JSONObject.toJSONStringWithDateFormat(obj,"yyyy-MM-dd");
        JSONObject json = JSONObject.parseObject(Jsonstr);
        b.setContent(json);
        return b;
    }


    public static BaseR returnOfMap(Map returnMap){
        BaseR b = baseModeBuild();
        b.setContent(returnMap);
        return b;
    }

    public static BaseR returnMsgOfString(String str){
        BaseR b = baseModeBuild();
        b.setMsg(str);
        return b;
    }

    public static BaseR returnOfString(String str){
        BaseR b = baseModeBuild();
        b.setContent(str);
        return b;
    }



    public static BaseR returnOfNull(){
        BaseR b = baseModeBuild();
        b.setContent("");
        return b;
    }


    public static BaseR errReturn(Object object, Logger logger){
        BaseR b = errBaseModeBuild();
        b.setContent(object);
        logger.error(object.toString());
        return b;
    }

    private static BaseR baseModeBuild(){
        BaseR b = new BaseR();
        b.setCode(0);
        b.setMsg("成功");
        b.setVersion(version);
        return b;
    }

    private static BaseR errBaseModeBuild(){
        BaseR b = new BaseR();
        b.setCode(1);
        b.setMsg("失败");
        b.setVersion(version);
        return b;
    }
}
